package tankgame06;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/16
 * @time 15:38
 *
 * 设计子弹
 */
public class Bullet implements Runnable {
    /**
     * 子弹x坐标
     */
    int x;
    int y;

    /**
     * 子弹方向
     */
    int direction;

    /**
     * 子弹速度
     */
    int speed = 2;
    /**
     * 子弹是否还存活
     */
    boolean isAlive = true;

    public Bullet(int x, int y, int direction) {
        this.x = x;
        this.y = y;
        this.direction = direction;
    }

    @Override
    public void run() {
        while (true){
            //线程创建后每休眠五十毫秒就修改一次坐标
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        switch (direction){
            case 0:
                y -= speed;
                break;
            case 1:
                x += speed;
                break;
            case 2:
                y += speed;
                break;
            case 3:
                x -= speed;
                break;
        }

            //子弹碰到边界就销毁 并且子弹不存活时退出
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750 && isAlive)){
                System.out.println("子弹线程退出");
                isAlive = false;
                //这里有break，所以上面判断条件是true
                break;
            }

        }
    }
}
